package uiLayer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;

public class Menu {
	private LinkedHashMap<String, Method> menuItems;
	private String title;
	private Class<?> c;
	
	public Menu(String title, Class<?> c){
		this.c = c;
		this.title = title;
		menuItems = new LinkedHashMap<String, Method>();
	}
	
	public void add(String description, String method) throws NoSuchMethodException, SecurityException{
		Method m = c.getDeclaredMethod(method, null);
		m.setAccessible(true);
		menuItems.put(description, m);
	}
	
	public Method run(){
		if(menuItems.size() > 0){
			Scanner keyboard = new Scanner(System.in);
			System.out.println("***" + title + "***");
			int i = 1;
			for(String s : menuItems.keySet()){
				System.out.println("[" + i + "] " + s);
				i++;
			}
			System.out.print("Pick: ");
			int pick = keyboard.nextInt();
			keyboard.nextLine();
			return menuItems.get(getKeyByIndex(pick));
		}else{
			throw new NullPointerException("There are no menu items!");
		}
	}
	
	private String getKeyByIndex(int index){
		String retS = null;
		int i = 1;
		for(String s : menuItems.keySet()){
			if(i == index){
				retS = s;
				break;
			}
			i++;
		}
		return retS;
	}
}
